From 2044513a3611cc524aa402d6a527b0aac0b8abba Mon Sep 17 00:00:00 2001 From: "Mr. E23" Date: Tue, 18 Nov 2003 23:43:41 +0000 Subject: [PATCH] Changed the way SQL queries are logged by profiling --- includes/DatabaseFunctions.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/includes/DatabaseFunctions.php b/includes/DatabaseFunctions.php index d753a016d9..753d2c8109 100644 --- a/includes/DatabaseFunctions.php +++ b/includes/DatabaseFunctions.php @@ -95,7 +95,11 @@ function wfEmergencyAbort( $msg = "" ) { function wfQuery( $sql, $db, $fname = "" ) { global $wgLastDatabaseQuery, $wgOut, $wgDebugDumpSql; - $profName = "wfQuery(\"" . strtr( substr($sql, 0, 80 ), "\t\n\r", " " ) . "...\")"; + + # wfGeneralizeSQL will probably cut down the query to reasonable + # logging size most of the time. The substr is really just a sanity check. + $profName = "wfQuery: " . substr( wfGeneralizeSQL( $sql ), 0, 255 ); + wfProfileIn( $profName ); if ( !is_numeric( $db ) ) { @@ -191,6 +195,18 @@ function wfInvertTimestamp( $ts ) { ); } +# Removes most variables from an SQL query and replaces them with X or N for numbers. +# It's only slightly flawed. Don't use for anything important. +function wfGeneralizeSQL( $sql ) +{ + # This could be done faster with some arrays and a single preg_replace, + # but this show more clearly what's going on. + $sql = preg_replace ( "/-?\d+/" , "N", $sql); + $sql = preg_replace ( "/([\'\"]).+?[^\\\\]\\1/", "\\1X\\1", $sql); + $sql = preg_replace ( "/\s+/", " ", $sql); + return $sql; +} + function wfFieldExists( $table, $field ) { $fname = "wfFieldExists"; -- 2.20.1